package com.anti.modular.sys.role.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.role.entity.SysRoleDataScope;
import com.anti.modular.sys.role.param.SysRoleParam;
import com.anti.modular.sys.role.service.SysRoleDataScopeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-11
 */
@Service
public class SysRoleDataScopeServiceImpl implements SysRoleDataScopeService {
    @Resource
    DBUtils dbUtils;

    @Override
    public void deleteRoleDataScopeListByOrgIdList(List<Long> orgIdList) {
        HashMap<Object, Object> map = CollectionUtil.newHashMap();
        String sql="delete from sys_role_data_scope where org_id in (:ids)";
        map.put("ids",orgIdList);
        dbUtils.execSql(sql,map);
    }

    @Override
    public List<Long> getRoleDataScopeIdList(List<Long> roleIdList) {
        String sql="select org_id from sys_role_data_scope where role_id in (:roleIds)";
        HashMap<Object, Object> param = CollectionUtil.newHashMap();
        param.put("roleIds",roleIdList);
        List<Long> list = dbUtils.query(sql, Long.class, param);
        return list;
    }

    @Override
    public void saveGrantDataScope(SysRoleParam sysRoleParam) {
        Long roleId = sysRoleParam.getId();
        //删除所拥有数据
        String removeSql="delete from sys_role_data_scope where role_id =? ";
        dbUtils.execSql(removeSql,roleId);
        //授权数据
        sysRoleParam.getGrantOrgIdList().forEach(orgId -> {
            SysRoleDataScope sysRoleDataScope = new SysRoleDataScope();
            sysRoleDataScope.setRoleId(roleId);
            sysRoleDataScope.setOrgId(orgId);
            dbUtils.insert(sysRoleDataScope);
        });
    }
}
